package panel.viewSpots
{
	import assists.SizeManager;
	
	import com.greensock.TweenLite;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.getTimer;
	public class StaticSlide
	{
		private static var _target:Sprite;
		private static var _stage:Stage;
		private static var _time:int;
		private static var _moveDis:int;
		private static var _touchID:int;
		private static var _prey:Number
		private static var _dragRec:Rectangle;
		private static var _oriInfoPoint:Point;
		private static var _moveMaxDis:int;
		private static var _startPosY:int;
		public static function beginSlide(target:Sprite,stage:Stage,rec:Rectangle = null,startPosY:int = 0):void
		{
			
			if(target.height <= stage.stageHeight * 0.7) return;
			if(startPosY == 0) startPosY = SizeManager.guideUpHeight;
			_startPosY = startPosY;
			if(rec != null)
			{
				_dragRec = rec;
			}else
			{
				_dragRec = new Rectangle(0,target.y,0,-target.height);
			}
		
			_target = target;
			_stage = stage;
			if(_target.y < 0)
			{
			_moveMaxDis = _dragRec.y - SizeManager.guideHeight;
//			_oriInfoPoint = new Point(target.x,_dragRec.y - SizeManager.guideDownHeight);
			_oriInfoPoint = new Point(target.x,_dragRec.y);
			}else
			{
				_oriInfoPoint = new Point(target.x,target.y);
			_moveMaxDis = _stage.stageHeight - SizeManager.guideHeight - _target.height - 20;
			}
			//		   桌面debug模式
			_target.addEventListener(MouseEvent.MOUSE_DOWN,clickHandler);
			
		}
		
		
		static protected function isMoveFar():void
		{
			if(_stage){
//				trace(_target.y + "---" + _target.height + "---")
				if(_target.y < _moveMaxDis)
				{
					_target.y = _moveMaxDis;
				}
				else if(_target.y > _startPosY)
				{
					_target.y = _startPosY;
				}
				
				if(Math.abs(_prey - _target.y) > 20)
				{
					_target.mouseChildren = false;
				}
			}
		}
		
		static protected function frameHandler(event:Event):void
		{
//			trace("frame" +　_target.y);
			isMoveFar();
		}
		
		static protected function upHandler(event:MouseEvent):void
		{
			//			Log.trace("stop mao pao1111");
			if(!_target) return;
			TweenLite.killTweensOf(_target);
			_stage.removeEventListener(MouseEvent.MOUSE_UP,upHandler);
			_target.removeEventListener(Event.ENTER_FRAME,frameHandler);
			_target.stopDrag();
			_time = (getTimer() - _time);
			_moveDis = event.stageY - _moveDis
			//			Log.trace(_moveDis + "-----" + _time + "=====" +  Math.abs(_moveDis) / _time);
			if(Math.abs(_moveDis) / _time > 1.1)
			{
				var movePos:Number = _moveDis/Math.abs(_moveDis) * _stage.stageHeight * (120 / _time);
				if(Math.abs(movePos) > _stage.stageHeight)
				{
					movePos = _moveDis/Math.abs(_moveDis) *_stage.stageHeight
				}
				if(_target.y +movePos < _moveMaxDis)
				{
					movePos = _moveMaxDis;
				}else if(_target.y +movePos > SizeManager.UpGuideHeight + _dragRec.y && _dragRec.y > 0)
				{
					movePos = SizeManager.UpGuideHeight + _dragRec.y;
				}else
				{
					movePos = _target.y + movePos;
				}
				
				if(Math.abs(movePos) > Math.abs(_oriInfoPoint.y) && _oriInfoPoint.y*movePos > 0)//当移动之后的位置超过初始pos位置的时候 纠正
				{
					movePos =  _oriInfoPoint.y;
				}
				else if(movePos <  _moveMaxDis)
				{
					movePos = _moveMaxDis;
				}else if(movePos > _startPosY)
				{
					movePos = _startPosY;
				}
				TweenLite.to(_target,0.6,{x:0,y:movePos});//TweenLite.to(_target,0.6,{x:0 + _target.parent.x,y:movePos + _target.parent.y});
				//				event.stopPropagation();
			}else if(_moveDis > 20 || _time > 600)
			{
				event.stopPropagation();
			}else
			{
				
			_target.dispatchEvent( new MouseEvent(MouseEvent.CLICK)); 
			}
			_time = 0;
			trace("up" +　_target.y);
		}
		
		static protected function clickHandler(event:MouseEvent):void
		{
			trace("down" +　_target.y);
			_target.mouseChildren = true;
			_prey = _target.y;
			_time = getTimer();
			_target.startDrag(false,_dragRec);
//			_target.y = _prey;
			_moveDis = event.stageY;
			_target.addEventListener(Event.ENTER_FRAME,frameHandler);
			_stage.addEventListener(MouseEvent.MOUSE_UP,upHandler);
			trace("down" +　_target.y);
		}		
		
		public static function clear(target:Sprite):void
		{
			if(target != _target) return;
			if(!_stage) return;
			_target.removeEventListener(MouseEvent.MOUSE_DOWN,clickHandler);
			if(_target.hasEventListener(Event.ENTER_FRAME))
			{
				_target.removeEventListener(MouseEvent.MOUSE_UP,upHandler);
				_target.removeEventListener(Event.ENTER_FRAME,frameHandler);
			}
			_target = null;
			_stage = null;
			_time = 0;
			_moveDis = 0;
		}
		
	}
}